package HtServer.FileUpload;

import HtServer.FileUpload.ImportMeetingFromExcel.ImportMeetingMenber;
import HtServer.RedisTools.RedisToos;
import HtServer.meeting.MeetingItems;
import HtServer.returnObject.ReturnObject;
import com.alibaba.fastjson.JSON;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.MultipartConfig;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.Part;

import java.io.*;

@MultipartConfig
@WebServlet(name = "FileUploadTempServlet", value = "/meetingFileUpload")
public class MeetingFileUploadX extends HttpServlet {

    private static final long serialVersionUID = 1L;
    private static final String UPLOAD_DIRECTORY = "uploads";
    // 上传配置
    private static final int MEMORY_THRESHOLD = 1024 * 1024 * 3;  // 3MB
    private static final int MAX_FILE_SIZE = 1024 * 1024 * 40; // 40MB
    private static final int MAX_REQUEST_SIZE = 1024 * 1024 * 50; // 50MB
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        response.setContentType("text/html");
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        // 获取文件上传的部分
        Part filePart = request.getPart("file");
        String userid = request.getParameter("userid");
        System.out.println("userid = "+userid);
        ReturnObject rto = new ReturnObject();

        // 获取上传的文件名
        String fileName = filePart.getSubmittedFileName();

        // 定义文件保存路径
        String filePath = getServletContext().getRealPath("/") + File.separator + "uploads" + File.separator + fileName;

        // 获取文件的输入流
//        try (InputStream fileContent = filePart.getInputStream();
//             FileOutputStream fileOutputStream = new FileOutputStream(filePath)) {
//            byte[] buffer = new byte[1024];
//            int bytesRead;
//            while ((bytesRead = fileContent.read(buffer)) != -1) {
//                fileOutputStream.write(buffer, 0, bytesRead);
//            }
//        }
        try (InputStream fileContent = filePart.getInputStream()) {
            byte[] buffer = fileContent.readAllBytes();
            ByteArrayInputStream bufferStream =new ByteArrayInputStream(buffer);
            ImportMeetingMenber importMeetingMenber = new ImportMeetingMenber();
            importMeetingMenber.MeetingMenberImages(bufferStream);
            bufferStream.reset();
            importMeetingMenber.ImportMeetingMenberCells(bufferStream);
            importMeetingMenber.meetingItems.forEach(System.out::println);
            RedisToos redisToos = RedisToos.getInstance();
            redisToos.getJedis().flushDB();
            importMeetingMenber.write2Rides();
            MeetingItems.getInstance().getMeetingItemFromRedis();
            MeetingItems.getInstance().sort();;

            PrintWriter writer = response.getWriter();
            rto.setMsg("上传成功");
            rto.setType(ReturnObject.SUCCESS);
            writer.write(JSON.toJSON(rto).toString());

            writer.flush();
            writer.close();
        }

        catch (Exception ex) {
            System.out.println(ex.getMessage());
            request.setAttribute("message",
                    "错误信息: " + ex.getMessage());


            rto.setMsg("上传失败");
            rto.setType(ReturnObject.ERROR);
            PrintWriter writer = response.getWriter();
            writer.write(JSON.toJSON(rto).toString());

            writer.flush();
            writer.close();
        }

//        PrintWriter writer = response.getWriter();
//        writer.println(JSON.toJSON(rto).toString());
//        writer.flush();
//        writer.close();



    }
}
